package com.ymatou.iapi.selleractivity.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.json.JSONArray;
import org.json.JSONObject;

import com.ymatou.iapi.productquery.compare.LoggerHelper;
import com.ymttest.business.service.AdminCallService;
import com.ymttest.utils.FileUtil;
import com.ymttest.utils.JsonBeanHelper;
import com.ymttest.utils.MapUtil;
import com.ymttest.utils.YMTDateUtil;

public class CreateData {
	public static void main(String args[]) {

		List<String> productAndCatalogs = FileUtil.readFileAsLine("D:\\compare\\data\\product_catatogid.txt");

		LoggerHelper.setfolderPath("D:\\compare\\data\\act\\");

		FileUtil.deleteFile(LoggerHelper.errorPath);
		FileUtil.deleteFile(LoggerHelper.logPath);
	
		
		Map<String,List<String>> productAndCatalogsMap = new HashMap<>();
		
		
		productAndCatalogs.forEach(ele->{
			String productId = ele.split(",")[0];
			String catalogId = ele.split(",")[1];
			
			
			if (productAndCatalogsMap.keySet().contains(productId)){
				productAndCatalogsMap.get(productId).add(catalogId);
			}else{
				List<String> catalogs = new ArrayList<>();
				catalogs.add(catalogId);
				productAndCatalogsMap.put(productId, catalogs);
			}
			
		});
		
		
		productAndCatalogsMap.forEach((x,y) -> {
			String applyMsg = "";
			try {
				// updateMap do nothing
				ApplyActivityCall activityCall = new ApplyActivityCall();
				JSONObject applyEffectiveActBean = activityCall.getJsonBean(null);

				String productId = x;
				List<String> catalogList = y;

				Map<String, Object> requestMap = new HashMap<String, Object>();
				requestMap.put("SellerId", 3383);
				requestMap.put("ActivityId", 7519);
				requestMap.put("ActivityProducts[0]->ProductId", productId);
				requestMap.put("ActivityProducts[0]->ApplyCatalogs[0]->CatalogId", catalogList.get(0));
				requestMap.put("ActivityProducts[0]->ApplyCatalogs[0]->ActivityStockNum", 100000);
				requestMap.put("ActivityProducts[0]->ApplyCatalogs[0]->ActivityPrice", 10);
				requestMap.put("ActivityProducts[0]->ApplyCatalogs[0]->Sku", null);
				requestMap.put("ActivityProducts[0]->ApplyCatalogs[0]->BkcKey", null);
				requestMap.put("ActivityProducts[0]->ApplyCatalogs[0]->ProductInActivityId", 0);

				for(int i=1;i<catalogList.size();i++){
					JSONArray CatalogListBean = applyEffectiveActBean.getJSONArray("ActivityProducts").getJSONObject(0).getJSONArray("ApplyCatalogs");
					CatalogListBean.put(i, new JSONObject(CatalogListBean.get(0).toString()));
					CatalogListBean.getJSONObject(i).put("CatalogId",catalogList.get(i));
					CatalogListBean.getJSONObject(i).put("ActivityStockNum",100000);
					CatalogListBean.getJSONObject(i).put("ActivityPrice",10);
					CatalogListBean.getJSONObject(i).put("Sku","");
					CatalogListBean.getJSONObject(i).put("BkcKey","");
					CatalogListBean.getJSONObject(i).put("ProductInActivityId",0);
				}
				
				
				JsonBeanHelper.updateJsonBean(applyEffectiveActBean, requestMap);

				activityCall.setData(applyEffectiveActBean);
				activityCall.callService();
				JSONObject ret = new JSONObject(activityCall.getReturnData());
				applyMsg = activityCall.getReturnData();
				if (ret.has("Data") && ret.getJSONObject("Data").has("ApplySuccessProducts")
						&& ret.getJSONObject("Data").getJSONArray("ApplySuccessProducts").length() > 0) {
					
					String inaid = ret.getJSONObject("Data").getJSONArray("ApplySuccessProducts").getJSONObject(0)
							.getString("ProductInActivityId");
					AdminCallService.approveProductInActivity("fengyifeng@ymatou.com", "1qaz2wsx", inaid, MapUtil
							.hashMap("productBeginTime", YMTDateUtil.getBeforeOrNextSecond(300), "productEndTime", "2019-8-3 15:08:50"));
					
					String msgSuccess = "product: " + productId +  ",inaid:" + inaid;
					System.out.println(msgSuccess);
					LoggerHelper.verifyEquals(true, true, msgSuccess, true);
					
				} else {
					LoggerHelper.failure(applyMsg, true);
				}

			} catch (Exception e) {
				LoggerHelper.failure(applyMsg, true);
			}
		});
	}

}
